package com.yeqian.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author : yeqian
 * @Date : 2021/1/4 11:20
 * <p>
 * 配置Druid数据源
 */
@Configuration
public class DruidConfig {

    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druidDataSouce() {
        return new DruidDataSource();
    }

    /**
     * 后台监控
     *
     * @param
     * @return
     * @author yeqian
     * @date 2021/1/4 11:23
     */
    @Bean
    public ServletRegistrationBean StatViewServlet() {
        ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
        //配置账号密码
        Map<String, String> map = new HashMap<>(2);
        //登录的密码和账号key是固定的
        map.put("loginUsername","admin");
        map.put("loginPassword","123456");
        //配置访问权限
        map.put("allow","");
        // 禁止谁能访问 map.put("zhangsan","192.168.11.123");
        bean.setInitParameters(map);
        return bean;
    }
}
